package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.Redisconstant;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-新增]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        //插入基本数据
        setMealDao.insert(setmeal);
        //存入到redis
        String img = setmeal.getImg();
        String key = Redisconstant.SETMEAL_PIC_DB_RESOURCES;
        redisTemplate.opsForSet().add(key,img);
        //调用方法
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
    }

    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmeal_id",setmealId);
                map.put("checkgroup_id",checkgroupId);
                setMealDao.setSetmealAndCheckGroup(map);

            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据套餐id查询数据
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        log.info("[套餐-根据套餐id查询数据]id：{}",id);
        return setMealDao.selectById(id);
    }

    /**
     * 套餐编辑
     * @param setmeal
     * @param checkGroupIds
     */
    @Transactional//开启事务
    @Override
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        //更新原有关系
        //删除原有关系
        setMealDao.deleteSetmealAndCheckGroupBySetmealId(setmeal.getId());
        //向中间表添加数据
        setSetmealAndCheckGroup(setmeal.getId(),checkGroupIds);
        //编辑套餐
        setMealDao.update(setmeal);
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        log.info("[套餐-根据id删除]id:{}",id);
        //校验套餐是否可以删除
        Long count = setMealDao.countSetmealBySetmealId(id);

        if (count > 0) {
            throw new BusinessRuntimeException("当前套餐有数据,不能删除！");
        }
        //删除原有关系
        setMealDao.deleteSetmealAndCheckGroupBySetmealId(id);
        //实际删除操作
        setMealDao.deleteById(id);

    }
}
